﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Helper;
using System.Windows.Media.Imaging;
using Model;
using Microsoft.Xna.Framework.Input.Touch;

namespace DBFM7
{   /// <summary>
    /// copyright:kklldog
    /// date:2011-08-29
    ///  qq:17014842
    /// </summary>
    public partial class ChannelTile : PhoneApplicationPage
    {
        public ChannelTile()
        {
            InitializeComponent();
            TouchPanel.EnabledGestures = GestureType.Tap| GestureType.HorizontalDrag;
            if (!PlayListHelper.IsInited)
            {
                this.imgGo.Visibility = Visibility.Collapsed;
                PlayListHelper.InitChannelComplete += new Action(PlayListHelper_InitChannelComplete);
                PlayListHelper.InitChannel();//初始化播放列表
            }
            else
                this.imgGo.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 初始化tile的image跟title
        /// </summary>
        void PlayListHelper_InitChannelComplete()
        {
            var list = PlayListHelper.GetEachChannel1stSong();//获取所有频道的第一首歌曲
            var song = list.Where(s => s.channel == (int)ChannelType.华语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                HY.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                HY.Title = ChannelType.华语.ToString();
                HY.Message=s.artist+"-"+s.title;
                HY.Tap+=new EventHandler<System.Windows.Input.GestureEventArgs>(this.HubTile_Tap);
            }
            else
            {
                FAV.Title = ChannelType.华语.ToString();
                FAV.Message = "No Song!";
            }


            song = list.Where(s => s.channel == (int)ChannelType.欧美).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                OM.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                OM.Title = ChannelType.欧美.ToString();
                OM.Message = s.artist + "-" + s.title;
                OM.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(this.HubTile_Tap);
            }
            else
            {
                FAV.Title = ChannelType.欧美.ToString();
                FAV.Message = "No Song!";
            }


            song = list.Where(s => s.channel == (int)ChannelType.韩语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                HanY.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                HanY.Title = ChannelType.韩语.ToString();
                HanY.Message = s.artist + "-" + s.title;
                HanY.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(this.HubTile_Tap);
            }
            else
            {
                FAV.Title = ChannelType.韩语.ToString();
                FAV.Message = "No Song!";
            }

            song = list.Where(s => s.channel == (int)ChannelType.日语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                RY.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                RY.Title = ChannelType.日语.ToString();
                RY.Message = s.artist + "-" + s.title;
                RY.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(this.HubTile_Tap);
            }
            else
            {
                FAV.Title = ChannelType.日语.ToString();
                FAV.Message = "No Song!";
            }

            song = list.Where(s => s.channel == (int)ChannelType.粤语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                YY.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                YY.Title = ChannelType.粤语.ToString();
                YY.Message = s.artist + "-" + s.title;
                YY.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(this.HubTile_Tap);
            }
            else
            {
                FAV.Title = ChannelType.粤语.ToString();
                FAV.Message = "No Song!";
            }

            song = list.Where(s => s.channel == (int)ChannelType.最爱).ToList();
            if (song != null&&song.Count>0)
            {
                var s = song[0];
                FAV.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                FAV.Title = ChannelType.最爱.ToString();
                FAV.Message = s.artist + "-" + s.title;
                FAV.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(this.HubTile_Tap);
            }
            else
            {
                FAV.Title = ChannelType.最爱.ToString();
                FAV.Message = "No Song!";
            }

            this.PageTitle.Text = "请选择一个你喜欢的频道";

        }

        private bool IsDo = false;
        private HubTile ht;
        private TurnstileTransition tt;
        private void TranToMainPage()
        {
            if (tt == null)
            {
                tt = new TurnstileTransition();
            }
            tt.Mode = TurnstileTransitionMode.ForwardOut;

            ITransition transitionToChannelTile = tt.GetTransition(this.LayoutRoot);
            transitionToChannelTile.Completed += (s, e) =>
                {
                    this.Visibility = Visibility.Collapsed;
                    transitionToChannelTile.Stop();
                    string pageUrl = "";
                    if (ht==null)
                    {
                        pageUrl = "/MainPage.xaml";
                    }
                    else if(ht!=null&&ht.Title!="")
                        pageUrl = "/MainPage.xaml?Channle=" + ht.Title;
                    this.NavigationService.Navigate(new Uri(pageUrl, UriKind.Relative));
                };
            transitionToChannelTile.Begin();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (PlayListHelper.IsInited)
            {
                PlayListHelper_InitChannelComplete();
            }
        }

        private void HubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            IsDo = false;
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                //点击
                if (gesture.GestureType == GestureType.Tap && !IsDo)
                {
                    IsDo = true;
                    ht = sender as HubTile;
                    if (ht != null)
                    {
                        TranToMainPage();

                    }
                }
            }
        }

        private void imgGo_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            TranToMainPage();
        }

        private void ChannelTile_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            if(MessageBox.Show("是否退出?","提示",MessageBoxButton.OKCancel)==MessageBoxResult.OK)
            {
                new Microsoft.Xna.Framework.Game().Exit(); 
            }
        }

    }
}